﻿using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using SouthernLand.Common;
using SouthernLand.Common.BOL.Photo;
using SouthernLand.Common.Untilities;
using File = SouthernLand.Common.BOL.Photo.File;

namespace SouthernLand.WebMaster.Photos
{
    public partial class Photos : Page
    {
        private int CateID = -1;
        private int FileID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {
            CateID = Convert.ToInt32(Request.QueryString["CateID"]);
            if (CateID > 0)
            {
                PhotoCategory obj = PhotoCategory.GetCateByCateID(CateID);
                lblAlbumsName.Text = obj.CateName;
            }

            FileID = ConvertUtility.ToInt32(Globals.GetIntFromQueryString("FileID"));
            if (!Page.IsPostBack)
                Load_FileByID(FileID);
        }

        protected void Load_FileByID(int fileId)
        {
            if (fileId > 0)
            {
                PhotoFile.Visible = false;
                fqfPhoto.Enabled = false;
                File obj = File.GetFileByFileID(fileId);
                if (obj != null)
                {
                    HPhotoFile.Value = obj.FileName.Trim();
                    PhotoTitle.Text = obj.Title.Trim();
                    txtDesc.Text = obj.Description.Trim();
                    ImageFile.ImageUrl = Globals.ApplicationPath + "Photos/Image.ashx?Size=M&ImageUrl=" +
                                         obj.FileName.Trim();
                }
            }
        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridViewItem.Rows.Count; i++)
            {
                GridViewRow row = GridViewItem.Rows[i];
                bool isChecked = ((CheckBox) row.FindControl("chkSelect")).Checked;
                if (isChecked)
                {
                    int ID = Convert.ToInt32(((CheckBox) row.FindControl("chkSelect")).ToolTip);
                    File.Delete(ID);
                }
            }

            GridViewItem.DataBind();
        }

        protected string Photo_Upload()
        {
            string strFileName = "";
            if (PhotoFile.HasFile)
            {
                string strExtension = Path.GetExtension(PhotoFile.FileName).ToUpper();
                if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".JPEG" ||
                    strExtension == ".PNG" || strExtension == ".BMP")
                {
                    strFileName = Common.Common.GetFileName() + Path.GetExtension(PhotoFile.FileName);
                    string strFilePathLarge = Request.PhysicalApplicationPath + "/UserUpload/Gallery/" + strFileName;
                    EnumUpload uploadLarge = FileProcess.UpLoadImage(PhotoFile, strFilePathLarge, 400);
                }
                else
                {
                    lblMsg.Text = "File bạn chọn upload không hợp lệ.";
                    return string.Empty;
                }
            }
            else
            {
                return HPhotoFile.Value.Trim();
            }

            return strFileName;
        }

        protected void PhotoAdd_Click(object sender, EventArgs e)
        {
            string fileName = Photo_Upload();

            if (!string.IsNullOrEmpty(fileName))
            {
                PhotoAdd(CateID, PhotoTitle.Text.Trim(), fileName);
                Response.Redirect("Photos.aspx?CateID=" + CateID);
            }
        }

        protected void PhotoAdd(int cateId, string title, string fileName)
        {
            var obj = new File();
            obj.FileID = FileID;
            obj.CateID = cateId;
            obj.Title = title;
            obj.FileName = fileName;
            obj.Description = txtDesc.Text.Replace(Environment.NewLine, "<br>");
            obj.FileSize = 0;
            obj.Height = 1;
            obj.Width = 1;
            obj.PostDate = DateTime.Now;
            obj.Username = "";
            obj.TotalViews = 0;
            File.Insert(obj);
        }
    }
}